/**
 * Created by Ysssssss on 20/12/14.
 */
const app = getApp();
import { navTo } from '../../../common/env';
import { TARGET_TYPE } from '../../../constants/common';
import { PACKAGE_PRODUCT_PAGE } from '../../../constants/route';
import { DEFAULT_GROUP_INFO } from '../../../constants/product';
import { getProductGroupList, getProductList, getStoreProductGroupList, getStoreProductList, getSupplyProductList } from '../../../api/product';

Component({
    externalClasses: ['component-class', 'list-class'],
    properties: {
        type: {
            type: String,
            value: TARGET_TYPE.brand, // brand品牌商品 store店铺商品 supply品牌供货库
        },
        checkedList: {
            type: Array,
            value: [],
        },
        showCheckboxButton: {
            type: Boolean,
            value: false
        },
        showOperationButton: {
            type: Boolean,
            value: false
        },
        hasFooter: {
            type: Boolean,
            value: true,
        },
    },
    data: {
        TARGET_TYPE,
        searchInput: '',
        searchValue: '',
        activeTab: '',
        groupList: [],
        refreshDate: Date.now(),
    },
    lifetimes: {
        async ready() {
            const { type } = this.data;
            let groupList = [];
            if (type === TARGET_TYPE.brand || type === 'supply') {
                groupList = await getProductGroupList();
            } else if (type === TARGET_TYPE.store) {
                groupList = await getStoreProductGroupList();
            }
            groupList = groupList.concat(DEFAULT_GROUP_INFO)
            this.setData({
                groupList,
                activeTab: groupList[0].productGroupId,
            }, () => {
                this.selectComponent('#tabs').resize();
            })
            app.event.addListener('productGroupActiveTabChange', this._productGroupActiveTabChange.bind(this))
            app.event.addListener('productGroupListChange', this._productGroupListChange.bind(this))
            app.event.addListener('productListRefresh', this._productListRefresh.bind(this))
        },
        detached() {
            app.event.removeListener('productGroupActiveTabChange', this._productGroupActiveTabChange.bind(this))
            app.event.removeListener('productGroupListChange', this._productGroupListChange.bind(this))
            app.event.removeListener('productListRefresh', this._productListRefresh.bind(this))
        }
    },
    methods: {
        _productGroupActiveTabChange(productGroupId) {
            this.setData({
                activeTab: productGroupId
            })
        },
        _productGroupListChange(groupList, refreshTabsRender) {
            const { activeTab } = this.data
            // tab组件 wx:for部分场景不会重新渲染，需要先清除再赋值
            refreshTabsRender && this.setData({
                groupList: [],
                activeTab: '',
            })
            groupList = groupList.concat(DEFAULT_GROUP_INFO);
            this.setData({
                groupList,
                activeTab: groupList.filter(group => group.productGroupId === activeTab).length ? activeTab : groupList[0].productGroupId
            }, () => {
                refreshTabsRender && this.selectComponent('#tabs').resize();
            })
        },
        _productListRefresh(refreshTabsRender) {
            this.setData({
                refreshDate: Date.now(),
            }, () => {
                refreshTabsRender && this.selectComponent('#tabs').resize();
            });
        },
        handleProductChecked(e) {
            this.triggerEvent('handleProductChecked', e.detail)
        },
        onSearchValueChange(e) {
            this.setData({
                searchInput: e.detail
            });
        },
        onSearch() {
            const { searchInput } = this.data
            this.setData({
                searchValue: searchInput
            })
        },
        onTabChange(e) {
            this.setData({
                activeTab: e.detail.name,
            });
        },
        navToProductGroupPage() {
            const { activeTab, type } = this.data;
            if (type === TARGET_TYPE.brand || type === 'supply') {
                navTo(PACKAGE_PRODUCT_PAGE['productGroupList'], {
                    value: activeTab,
                });
            } else if (type === TARGET_TYPE.store) {
                navTo(PACKAGE_PRODUCT_PAGE['storeProductGroupList'], {
                    value: activeTab,
                });
            }
        },
        async getProductListAction(e) {
            const [params, callback] = e.detail;
            const { type } = this.data;
            let list = [];
            if (type === TARGET_TYPE.brand) {
                list = await getProductList(params);
            } else if (type === 'supply') {
                list = await getSupplyProductList(params);
            } else if (type === TARGET_TYPE.store) {
                list = await getStoreProductList(params)
            }
            callback && callback(list)
        },
        // 更改佣金比例
        handleChangePercentOff(e) {
            this.triggerEvent('changePercentOff', e.detail);
        },
    }
})